//
//  STTools_WaitingNetViewController.m
//  WithHeart
//
//  Created by imac on 2020/1/3.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "STTools_WaitingNetViewController.h"
#import "STTools_GuideViewController.h"
@interface STTools_WaitingNetViewController ()
@property(nonatomic, strong) STLabel                     *lable;/**<  */
@end

@implementation STTools_WaitingNetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self stoneobs_ui_configSubView];
    __weak typeof(self) weakSelf =  self;
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
            [weakSelf sendUpdatemtRequest];
        }else{
            weakSelf.lable.hidden = NO;
        }
    }];
    [manager startMonitoring];
    // Do any additional setup after loading the view.
}
- (void)judgeWhereToGo{
    
    BOOL show = [NSUserDefaults.standardUserDefaults boolForKey:SHOW_WEALCOME_KEY];
    
    if (show) {
        UIWindow * window =  UIApplication.sharedApplication.delegate.window ;
        window.rootViewController = STTools_TabbarViewController.new;
        
    }else{
      UIApplication.sharedApplication.delegate.window.rootViewController = STTools_GuideViewController.new;
    }
    
    
}
#pragma mark --stoneobs_ui_configSubView
- (void)stoneobs_ui_configSubView{
    self.navigationController.navigationBar.hidden = YES;
    self.tableView.contentInset = UIEdgeInsetsZero;
    
    UIImageView * header = [[UIImageView alloc] initWithFrame:STONEOBS_UI_UISCREEN_FRAME];
    header.image = [UIImage imageNamed:@"happy_game_qd_6"];
    self.tableView.tableHeaderView = header;
    
    STLabel * titleLabel = [[STLabel alloc] initWithFrame:CGRectMake(15, TMUtils.stoneobs_fun_get_tabBarTop - 60, STONEOBS_UI_UISCREEN_WIDTH - 30, 30)
                                                     text:@"网络连接失败，请检查您的网络是否可用,或者请允许App使用网络权限"
                                                textColor:STONEOBS_UI_FIRST_TEXT_COLOR
                                                     font:13
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [NSString stoneobs_fun_lableAutoAdjustheightWithLabel:titleLabel];
    [header addSubview:titleLabel];
    self.lable = titleLabel;
    titleLabel.hidden = YES;
    
    
}
- (void)sendUpdatemtRequest{
    NSMutableDictionary * paramDic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"index/mt"];
    [paramDic setObject:@"ios" forKey:@"user_from"];
    NSString * version = [NSString stringWithFormat:@"%@",UIApplication.stoneobs_fun_st_applictionVersin];
    [paramDic setObject:version forKey:@"version"];
    [paramDic setObject:@"apple" forKey:@"register_name"];
    DDLogInfo(@"url:%@",url);
    DDLogInfo(@"param:%@",paramDic);
    AFHTTPSessionManager * manger = [AFHTTPSessionManager manager];
    manger.requestSerializer = [AFHTTPRequestSerializer serializer];
    manger.requestSerializer.timeoutInterval = 20;
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    
    [manger GET:url parameters:paramDic headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary * dataDic = responseObject[@"data"];
        NSString * code = [responseObject[@"code"] description];
        if (code.integerValue == 1) {
            NSString  *pg_zzsh_des = [dataDic[@"pg_zzsh"] description];
            NSInteger pg_zzsh = 1;
            if ([pg_zzsh_des isEqualToString:@"stonobesmeiyouheishui"]) {
                pg_zzsh = 0;
            }

            [self registerRCINfoWithPg_zzsh:pg_zzsh];
        }
        [self judgeWhereToGo];

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self sendUpdatemtRequest];
        });
    }];
    
}
- (void)registerRCINfoWithPg_zzsh:(BOOL)pg_zzsh{
    NSString * rc_key = RC_DEBUG_APP_KEY;
    [[STTools_RcImManger manger] registerAllCustomModel];
    [[RCIM sharedRCIM] setReceiveMessageDelegate:STTools_MessageListener.manger];
}
@end
